package com.interview.mapper;

import com.interview.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author JiangYingbo
 */
@Repository
public interface UserMapper {
    /**
     * 按条件查询用户记录
     *
     * @param condition 查询条件
     * @return 查询失败时返回空List对象
     */
    @Select("<script>" + "select u.user_id, u.nick_name, u.phone, u.email, u.username, " +
            "u.password, u.gender, u.age, u.completed_ques, u.avatar_url, u.job_interest, " +
            "u.tech_tack, u.create_time, u.modify_time " +
            "from interview_ai.user u " +
            "<where> " +
            "<if test='userId != null'>and u.user_id = #{userId}</if> " +
            "<if test='nickName != null'>and u.nick_name = #{nickName}</if> " +
            "<if test='phone != null'>and u.phone = #{phone}</if> " +
            "<if test='email != null'>and u.email = #{email}</if> " +
            "<if test='username != null'>and u.username = #{username}</if> " +
            "<if test='password != null'>and u.password = #{password}</if> " +
            "<if test='gender != null'>and u.gender = #{gender}</if> " +
            "<if test='age != null'>and u.age = #{age}</if> " +
            "<if test='completedQues != null'>and u.completed_ques = #{completedQues}</if> " +
            "<if test='avatarUrl != null'>and u.avatar_url = #{avatarUrl}</if> " +
            "<if test='jobInterest != null'>and u.job_interest = #{jobInterest}</if> " +
            "<if test='techTack != null'>and u.tech_tack = #{techTack}</if> " +
            "<if test='createTime != null'>and u.create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and u.modify_time = #{modifyTime}</if> " +
            "</where> " +
            "</script> ")
    List<User> select(User condition);

    /**
     * 新增用户
     *
     * @param user 用户
     * @return 新增用户数
     */
    @Insert("insert into user (" +
            "nick_name, " +
            "phone, " +
            "email, " +
            "username, " +
            "password, " +
            "avatar_url, " +
            "job_interest, " +
            "tech_tack) " +
            "values (#{nickName}, #{phone}, #{email}, #{username}, #{password}, #{avatarUrl}, #{jobInterest}, #{techTack})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insert(User user);

    /**
     * 修改用户信息
     *
     * @param user 用户信息
     * @return 修改受影响行数
     */
    @Update("update user set " +
            "nick_name=#{nickName}, " +
            "phone=#{phone}, " +
            "email=#{email}, " +
            "username=#{username}, " +
            "gender=#{gender}, " +
            "age=#{age}, " +
            "job_interest=#{jobInterest}, " +
            "tech_tack=#{techTack} " +
            "where user_id=#{userId}"
    )
    int update(User user);
}
